package com.ifast.experts.service.impl;

import com.ifast.experts.domain.TreeNode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ifast.experts.dao.ExpertExpertsInfoDao;
import com.ifast.experts.domain.ExpertExpertsInfoDO;
import com.ifast.experts.service.ExpertExpertsInfoService;
import com.ifast.common.base.CoreServiceImpl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 
 * <pre>
 * 
 * </pre>
 * <small> 2018-12-05 12:02:33 | JimmyMo</small>
 */
@Service
public class ExpertExpertsInfoServiceImpl extends CoreServiceImpl<ExpertExpertsInfoDao, ExpertExpertsInfoDO> implements ExpertExpertsInfoService {

    @Autowired
    private ExpertExpertsInfoDao expertExpertsInfoDao;

    public List<TreeNode> getDicList(Map<String,String> param){
        return expertExpertsInfoDao.getDicList(param);
    }

    public List<TreeNode> arrListToTree(List<TreeNode> arrList){
        List<TreeNode> finalRes = new ArrayList<>();
        for (TreeNode tree : arrList) {
            if (tree.getPid().equals("0")) {
                finalRes.add(findChildren(tree, arrList));
            }
        }
        return finalRes;
    }

    private TreeNode findChildren(TreeNode tree, List<TreeNode> list) {
        for (TreeNode node : list) {
            if (node.getPid().equals(tree.getId())) {
                if (tree.getChildren() == null) {
                    tree.setChildren(new ArrayList<TreeNode>());
                }
                tree.getChildren().add(findChildren(node, list));
            }
        }
        return tree;
    }

}
